<?php

use Jialeo\LaravelSchemaExtend\Schema;
use Illuminate\Database\Schema\Blueprint;
use App\Libraries\DataBase\Migrations\BaseMigration;

class Auth extends BaseMigration
{
    public $prefix = 'auth_';

    /**
     * Run the migrations.
     */
    public function up()
    {
        if (!Schema::hasTable($this->prefix.'modules')) {
            Schema::create($this->prefix.'modules', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->comment = '模块信息';
                $table->uuid('module_id')->comment('模块主键');
                $table->primary('module_id');

                $table->uuid('parent_id')->nullable()->comment('父级主键')->default(0);
                $table->string('encode')->comment('编码');
                $table->string('full_name')->comment('名称');
                $table->string('icon')->nullable()->comment('图标');
                $table->string('url_address')->nullable()->comment('导航地址');
                $table->string('params')->nullable()->comment('导航参数');
                $table->string('target')->nullable()->comment('导航目标');
                $table->integer('is_menu')->default(1)->comment('菜单选项')->default(0);
                $table->integer('is_public')->default(1)->comment('是否公开')->default(0);
                $table->integer('expand_enable')->default(1)->comment('允许展开')->default(0);

                $this->hasDescription($table);
                $this->hasEditEnable($table);
                $this->hasSortCode($table);
                $this->hasStatus($table);
                $this->hasCreated($table);
                $this->hasUpdated($table);
            });
        }
        if (!Schema::hasTable($this->prefix.'module_buttons')) {
            Schema::create($this->prefix.'module_buttons', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->uuid('module_button_id')->comment('按钮主键');
                $table->primary('module_button_id');
                $table->uuid('module_id')->comment('功能编号');
                $table->uuid('parent_id')->nullable()->comment('父级编号');
                $table->string('icon')->nullable()->comment('图标');
                $table->string('encode')->comment('编码');
                $table->string('full_name')->comment('名称');
                $table->string('action_address')->nullable()->comment('Action地址');

                $this->hasSortCode($table);
                $this->hasDescription($table);
            });
        }
        if (!Schema::hasTable($this->prefix.'module_columns')) {
            Schema::create($this->prefix.'module_columns', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->uuid('module_column_id')->comment('列主键');
                $table->primary('module_column_id');
                $table->uuid('module_id')->comment('功能主键');
                $table->uuid('parent_id')->nullable()->comment('父级主键');
                $table->string('encode')->comment('	编码	');
                $table->string('full_name')->comment('名称');

                $this->hasSortCode($table);
                $this->hasDescription($table);
            });
        }
        if (!Schema::hasTable($this->prefix.'user_relations')) {
            Schema::create($this->prefix.'user_relations', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->string('user_id')->comment('用户主键');
                $this->hasPolymorphic($table, 'categorytable', '对象相关表');
                $table->unsignedTinyInteger('is_default')->comment('默认对象');

                $this->hasSortCode($table);
                $table->timestamps();
            });
        }
        if (!Schema::hasTable($this->prefix.'authorizes')) {
            Schema::create($this->prefix.'authorizes', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $this->hasPolymorphic($table, 'categorytable', '对象相关表');
                $this->hasPolymorphic($table, 'itemtable', '项目相关表');

                $this->hasSortCode($table);
                $table->timestamps();
            });
        }
        if (!Schema::hasTable($this->prefix.'roles')) {
            Schema::create($this->prefix.'roles', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->uuid('role_id')->comment('角色编号');
                $table->primary('role_id');

                $table->uuid('organize_id')->nullable()->comment('机构主键')->default('');
                $table->integer('category')->default(1)->comment('分类1-角色;');
                $table->string('encode')->comment('角色编码');
                $table->string('name')->comment('角色名称');
                $table->integer('is_public')->comment('公共角色')->default(0);
                $table->dateTime('overdue_time')->nullable()->comment('过期时间');

                $this->hasStatus($table);
                $this->hasSortCode($table);
                $this->hasDescription($table);
                $table->timestamps();
                $table->softDeletes();
            });
        }
        if (!Schema::hasTable($this->prefix.'managers')) {
            Schema::create($this->prefix.'managers', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->uuid('manager_id')->comment('管理员编号');
                $table->primary('manager_id');

                $table->string('name')->nullable()->comment('姓名');
                $table->string('username')->comment('用户名');
                $table->string('password')->comment('密码');
                $table->integer('sex')->default(1)->comment('性别');
                $table->string('mobile')->nullable()->comment('手机');
                $table->string('email')->comment('邮箱');
                $table->rememberToken();

                $this->hasStatus($table);
                $this->hasDescription($table);
                $table->timestamps();
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::dropIfExists($this->prefix.'user_relations');
        Schema::dropIfExists($this->prefix.'authorizes');
        Schema::dropIfExists($this->prefix.'roles');
        Schema::dropIfExists($this->prefix.'managers');
        Schema::dropIfExists($this->prefix.'modules');
        Schema::dropIfExists($this->prefix.'module_buttons');
        Schema::dropIfExists($this->prefix.'module_columns');
    }
}
